/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;



import java.sql.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Vector;

public class ViewMembers extends JFrame implements ActionListener
{
    public Statement stmt1;
    public ResultSet rset;
    public String query1;
    private JLabel header;
    private JButton back;
    private JButton select;
    private JScrollPane scroll;
    private JTable table;
    private JLabel viewall;
    private JPanel viewmem;

    public ViewMembers()
    {
        super("Brickroad Fitness - View Members");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

        private void initComponents()
        {
            cnd a = new cnd();
            try {
                a.connect();
                query1 = "SELECT mID, mFirstname, mLastname, mStatus, mExpiration, mContact, mLastCheckin, mBalance, mActive FROM `member` ORDER BY `mID`";
                stmt1 = a.con1.createStatement();
                rset = stmt1.executeQuery(query1);
                ResultSetMetaData md = rset.getMetaData();
                int columnCount = md.getColumnCount();

                Vector columns = new Vector(columnCount);

                //store column names
                //for(int i=1; i<=columnCount; i++)
                //    columns.add(md.getColumnName(i));

                columns.add("Member ID");
                columns.add("First Name");
                columns.add("Last Name");
                //columns.add("Address");
                //columns.add("Gender");
                columns.add("Membership Status");
                columns.add("Membership Expiration");
                columns.add("Contacts");
                columns.add("Last Check-in");
                //columns.add("Date Joined");
                columns.add("Balance");
                columns.add("Activity Status");
     
                Vector data = new Vector();
                Vector row;

                //store row data
                while(rset.next())
                {
                    row = new Vector(columnCount);
                    for(int i=1; i<=columnCount; i++)
                        row.add(rset.getString(i));
                    data.add(row);
                }
                System.out.println("Records from member table were successfully copied.");
                table = new JTable(data, columns) {
                    public boolean isCellEditable(int row, int column) {
                        //all cells false
                        return false;
                    }
                };
                a.disconnect();
            }
            catch (Exception ex)
            {
                JOptionPane invalidsearch = new JOptionPane();
                invalidsearch.showMessageDialog(viewmem, "Error copying records from member table. " + ex.getMessage());
                System.out.println("Error copying records from member table. " + ex.getMessage());
            }

        viewmem = new JPanel();
        viewall = new JLabel();
        back = new JButton();
        select = new JButton();
        header = new JLabel();
        scroll = new JScrollPane();

        viewmem.setBackground(new java.awt.Color(255, 255, 102));
        viewmem.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));

        viewall.setFont(new java.awt.Font("Arial", 3, 18)); // NOI18N
        viewall.setText("View All Members");

        table.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        /*table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {new Integer(123456), "Lau", "Jamie", "Ty", "Female", "Member", "13 August 2012", "8 August 2011", new Integer(5476582), new Integer(75500)},
                {new Integer(123457), "Dela Cruz", "Ria", "Rodette", "Male", "Not Member", "15 May 2010", "10 May 2011", new Integer(4501563), new Integer(0)},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Membership ID", "Last Name", "First Name", "Middle Name", "Gender", "Membership Status", "Membership Expiration", "Last Check-in", "Contacts", "Balance"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });*/
        table.setGridColor(new Color(0, 0, 0));
        table.setRowHeight(20);

        scroll.setViewportView(table);
        table.getColumnModel().getColumn(0).setMinWidth(85);
        table.getColumnModel().getColumn(3).setMinWidth(50);
        table.getColumnModel().getColumn(5).setMinWidth(80);
        table.getColumnModel().getColumn(8).setMinWidth(100);

        back.setFont(new java.awt.Font("Century Gothic", 1, 18)); // NOI18N
        back.setText("BACK");
        back.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        back.addActionListener(this);

        select.setFont(new java.awt.Font("Century Gothic", 1, 18)); // NOI18N
        select.setText("SELECT");
        select.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        select.addActionListener(this);

        GroupLayout viewmemLayout = new GroupLayout(viewmem);
        viewmem.setLayout(viewmemLayout);
        viewmemLayout.setHorizontalGroup(
            viewmemLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(viewmemLayout.createSequentialGroup()
                .addGroup(viewmemLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(viewmemLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(back, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                        .addComponent(select, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
                    .addGroup(GroupLayout.Alignment.LEADING, viewmemLayout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(viewmemLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(viewall)
                            .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 886, GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        viewmemLayout.setVerticalGroup(
            viewmemLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(viewmemLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(viewall)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 347, GroupLayout.PREFERRED_SIZE)
                .addGroup(viewmemLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(back, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addComponent(select, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
                .addContainerGap())
        );

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(header, GroupLayout.PREFERRED_SIZE, 958, GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(viewmem, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header)
                .addGap(18, 18, 18)
                .addComponent(viewmem, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        if (event == back)
        {
            dispose();
            new Main();
        }
        else if (event == select)
        {
            if(table.getSelectedRow() != -1)
            {
                dispose();
                new MemberProfile(table.getValueAt(table.getSelectedRow(), 0).toString());
            }
        }
    }
}
